

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/resource/img/favicon.png">
  <link rel="icon" type="image/png" href="/resource/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="">
  <meta name="author" content="soldier">
  <meta name="keywords" content="">
  <title>请谈谈你对volatile的理解 - 黄结的个人博客</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/github-gist.min.css" />
    
  

  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.2.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>soldier</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
              <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                
                  
                  
                  
                  <a class="dropdown-item" href="/about/">
                    
                    个人简历
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://soldiergit.github.io/honor/">
                    
                    项目及获奖
                  </a>
                
              </div>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner intro-2" id="background" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container page-header text-center fade-in-up">
            <span class="h2" id="subtitle">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2020-11-03 10:44" pubdate>
        2020年11月3日 上午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      2.4k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      28
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto" id="post">
            <!-- SEO header -->
            <h1 style="display: none">请谈谈你对volatile的理解</h1>
            
            <div class="markdown-body" id="post-body">
              <h2 id="volatile是java虚拟机提供的轻量级的同步机制"><a href="#volatile是java虚拟机提供的轻量级的同步机制" class="headerlink" title="volatile是java虚拟机提供的轻量级的同步机制"></a>volatile是java虚拟机提供的轻量级的同步机制</h2><ol>
<li>保证可见性</li>
<li><font color=#FF000>不保证原子性</font></li>
<li>禁止命令重排<br>首先我们要懂得：JVM是java虚拟机、JMM是java内存模型。JMM（java内存模型Java Memory Model，简称JMM）本身是一个抽象的概念<font color=#FF000>并不真是存在</font>，它描述的是一组规则或规范，通过这组规范定义了程序中各个变量（包括实例字段，静态字段和构成数组对象的元素）的访问方式。</li>
</ol>
<p><strong><em>JMM关于同步的规定：</em></strong><br><strong>1.</strong> 线程解锁前，必须把共享变量的值刷新回主内存<br><strong>2.</strong> 线程加锁前，必须读取主内存的最新值到自己的工作内存<br><strong>3.</strong> 加锁解锁是同一把锁</p>
<p>由于JVM运行程序的实体是线程，而每个线程创建时JVM都会为其创建一个工作内存（有些地方称为栈空间），工作内存是每个线程的私有数据区域，而Java内存模型（JMM）中规定所有的变量都存储在<font color=#FF000>主内存</font>，主内存是共享内存区域，所有线程的可以访问，<font color=#FF000>但线程对变量的操作（读取赋值等）必须在工作内存中进行，首先要将变量从主内存拷贝到自己的工作内存空间，然后对变量进行操作，操作完成后再将变量写回主内存</font>，不能直接操作主内存中的变量，各个线程中的工作内存中存储着主内存中的<font color=#0000F>变量副本拷贝</font>，因此不同的线程间无法访问对方的工作内存，线程间的通信（传值）必须通过主内存来完成，其简要访问过程如下图：<br><img src="/resource/img/java/volatile.png" srcset="/img/loading.gif"></p>
<h2 id="JMM你谈谈"><a href="#JMM你谈谈" class="headerlink" title="JMM你谈谈"></a>JMM你谈谈</h2><h3 id="JMM的可见性"><a href="#JMM的可见性" class="headerlink" title="JMM的可见性"></a>JMM的可见性</h3><ul>
<li>假设主内存中有一个Student对象，它的age = 25，现在有3个线程（t1、t2、t3）要操作age，3个线程首先将25拷贝回自己的工作内存（变量拷贝），线程t1在自己的工作内存中将age改为了37，之后t1将这个37写回主内存中，而此时此刻主内存的age是37 线程t2、t3的age是25</li>
<li>我们必须要有一种机制，只要有一个线程修改完自己工作内存的值并写回主内存，要及时通知其它线程，而这个及时通知就是<font color=#FF000>JMM内存模型的第一种特性：可见性</font>，只要有变动，大家立马可见，知道最新消息。</li>
<li>通过前面对JMM的介绍，我们知道：各个线程对主内存中共享变量的操作都是各个线程各自拷贝到自己的工作内存进行操作后再写回到主内存中的。这就可能存在一个线程A修改了共享变量X的值但还未写回主内存中时，另一个线程B又对主内存中的同一个共享变量X进行操作，但此时A线程工作内存中共享变量X对线程B来说是不可见的，这种工作内存与主内存同步延迟现象就造成了可见性问题。</li>
</ul>
<h3 id="VolatileDemo代码演示可见性"><a href="#VolatileDemo代码演示可见性" class="headerlink" title="VolatileDemo代码演示可见性"></a>VolatileDemo代码演示可见性</h3><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.util.concurrent.TimeUnit;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyData</span> </span>&#123;
    <span class="hljs-keyword">volatile</span> <span class="hljs-keyword">int</span> number = <span class="hljs-number">0</span>;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">add</span><span class="hljs-params">()</span> </span>&#123;
        <span class="hljs-keyword">this</span>.number = <span class="hljs-number">60</span>;
    &#125;
&#125;

<span class="hljs-comment">/**</span>
<span class="hljs-comment"> * 1、验证volatile的可见性</span>
<span class="hljs-comment"> *  1.1 假设 int number = 0;，number变量之前根本没有添加volatile关键字修饰</span>
<span class="hljs-comment"> */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">VolatileDemo</span> </span>&#123;

    <span class="hljs-comment">// main方法本身算一个main线程</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;
        MyData myData = <span class="hljs-keyword">new</span> MyData();

        <span class="hljs-comment">// 创建一个新线程</span>
        <span class="hljs-keyword">new</span> Thread(() -&gt; &#123;
            System.out.println(Thread.currentThread().getName() + <span class="hljs-string">&quot;\t come in&quot;</span>);
            <span class="hljs-comment">// 暂停3秒钟后改变number的值</span>
            <span class="hljs-keyword">try</span> &#123;
                TimeUnit.SECONDS.sleep(<span class="hljs-number">3</span>);
            &#125; <span class="hljs-keyword">catch</span> (InterruptedException e) &#123;
                e.printStackTrace();
            &#125;
            myData.add();
            System.out.println(Thread.currentThread().getName() + <span class="hljs-string">&quot;\t update number value：&quot;</span> + myData.number);
        &#125;, <span class="hljs-string">&quot;AAA&quot;</span>).start();


        <span class="hljs-comment">// 第二个线程就是我们的main线程了</span>
        <span class="hljs-keyword">while</span> (myData.number == <span class="hljs-number">0</span>) &#123;
            <span class="hljs-comment">// main线程会一直在这里等待循环，直到number不为0</span>
        &#125;
        System.out.println(Thread.currentThread().getName() + <span class="hljs-string">&quot;\t mission is over，main get number value：&quot;</span> + myData.number);
    &#125;
&#125;</code></pre>

<h3 id="原子性"><a href="#原子性" class="headerlink" title="原子性"></a>原子性</h3><p><font color=#FF000><strong>number++在多线程下是非线程安全的</strong></font>：因为该操作不具备原子性。<br><strong>原子性</strong>是指不可分割，完整性，即某个线程正在做某个业务时，中间不可以被加塞或者被分割。需要整体完整，要么同时成功，要么同时失败。</p>
<h3 id="VolatileDemo不保证原子性代码演示"><a href="#VolatileDemo不保证原子性代码演示" class="headerlink" title="VolatileDemo不保证原子性代码演示"></a>VolatileDemo不保证原子性代码演示</h3><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.util.concurrent.TimeUnit;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyData</span> </span>&#123;
    <span class="hljs-keyword">volatile</span> <span class="hljs-keyword">int</span> number = <span class="hljs-number">0</span>;

    <span class="hljs-comment">// 注意：此时number前面是加了volatile关键字修饰的，volatile不保证原子性</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">addPlus</span><span class="hljs-params">()</span> </span>&#123;
        number++;
    &#125;
&#125;

<span class="hljs-comment">/**</span>
<span class="hljs-comment"> * 2、验证volatile不保证原子性</span>
<span class="hljs-comment"> *  1.1 原子性指的是什么？</span>
<span class="hljs-comment"> *      不可分割，完整性，即某个线程正在做某个业务时，中间不可以被加塞或者被分割。需要整体完整，要么同时成功，要么同时失败。</span>
<span class="hljs-comment"> */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">VolatileDemo</span> </span>&#123;

    <span class="hljs-comment">// main方法本身算一个main线程</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;

        MyData myData = <span class="hljs-keyword">new</span> MyData();

        <span class="hljs-comment">// 20个线程，每个线程++1000次，理想值是20000</span>
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt;= <span class="hljs-number">20</span>; i++) &#123;
            <span class="hljs-keyword">new</span> Thread(() -&gt; &#123;
                <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">1</span>; j &lt;= <span class="hljs-number">1000</span>; j++) &#123;
                    myData.addPlus();
                &#125;
            &#125;, String.valueOf(i)).start();
        &#125;

        <span class="hljs-comment">// 需要等待上面20个线程都全部计算完成后，再用main线程取得最终结果值</span>
        <span class="hljs-keyword">while</span> (Thread.activeCount() &gt; <span class="hljs-number">2</span>) &#123; <span class="hljs-comment">// 返回活动线程的当前线程的线程组中的数量</span>
            Thread.yield(); <span class="hljs-comment">// 声明当前线程已经完成了生命周期中最重要的部分，可以切换给其它线程来执行。该方法只是对线程调度器的一个建议，而且也只是建议具有相同优先级的其它线程可以运行。</span>
        &#125;
        System.out.println(Thread.currentThread().getName() + <span class="hljs-string">&quot;\t finally number value：&quot;</span> + myData.number);

    &#125;
&#125;</code></pre>
<p><strong><em><font color=#FF000>为什么volatile不保证原子性？</font></em></strong><br>&nbsp;&nbsp;因为会出现丢失写值的情况，一个线程在写会主内存时，其它线程被挂起，而volatile保证可见性，主内存还没来得及通知其它线程，另一个线程就它自己工作内存的值写进来了。</p>
<h3 id="有序性"><a href="#有序性" class="headerlink" title="有序性"></a>有序性</h3><p>&nbsp;&nbsp;计算机在执行程序时，为了提高性能，编译器和处理器常常会对<font color=#FF000>指令做重排</font>，一般分为一下3种：<br><img src="/resource/img/java/JMM_rearrangement.png" srcset="/img/loading.gif"></p>
<ol>
<li>单线程环境里面确保程序最终执行结果和代码顺序执行的结果一致。即<font color=#FF000>单线程环境下不用担心指令重排</font>；</li>
<li>处理器在进行重排序时必须要考虑指令之间的<font color=#FF000><strong>数据依赖性</strong></font>；</li>
<li>多线程环境中线程交替执行，由于编译器优化重排的存在，两个线程中使用的变量能否保证一致性是无法确定的，结果无法预测。<br>比如说：现有 int a,b = 0;<br>按顺序，线程t1执行 a = b，线程2执行 b = 2;<br>理论上的结果是：a=0 b=2<br>由于在多线程环境下，编译器会自己做优化，对你写的代码进行重排序，会导致 a=2 b=2</li>
</ol>
<h3 id="线程安全性保证"><a href="#线程安全性保证" class="headerlink" title="线程安全性保证"></a>线程安全性保证</h3><p><strong>1、</strong> 工作内存与主内存同步延迟现象导致的可见性问题：<br>&nbsp;&nbsp;可以使用synchronized或volatile关键字解决，它们可以是一个线程<font color=#FF000>修改后的变量立即对其它线程可见</font>。<br><strong>2、</strong> 对于指令重排导致的可见性问题和有序性问题：<br>&nbsp;&nbsp;可以利用volatile关键字解决，因为volatile的另一个作用就是禁止重排序优化。</p>
<h2 id="你在哪些地方用过volatile"><a href="#你在哪些地方用过volatile" class="headerlink" title="你在哪些地方用过volatile"></a>你在哪些地方用过volatile</h2><h3 id="单例模式DCL代码"><a href="#单例模式DCL代码" class="headerlink" title="单例模式DCL代码"></a>单例模式DCL代码</h3><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SingletonDemo</span> </span>&#123;

    <span class="hljs-comment">// 单例模式</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">volatile</span> SingletonDemo instance = <span class="hljs-keyword">null</span>;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">SingletonDemo</span><span class="hljs-params">()</span> </span>&#123;
        System.out.println(Thread.currentThread().getName() + <span class="hljs-string">&quot;\t 我是构造函数SingletonDemo()&quot;</span>);
    &#125;

    <span class="hljs-comment">// DCL（Double Check Lock）:双端检锁机制</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> SingletonDemo <span class="hljs-title">getInstance</span><span class="hljs-params">()</span> </span>&#123;
        <span class="hljs-keyword">if</span> (instance == <span class="hljs-keyword">null</span>) &#123;
            <span class="hljs-comment">// 没加volatile前 可能打印：2   我是构造函数SingletonDemo()</span>
            <span class="hljs-comment">// 加了volatile后 只会打印：1   我是构造函数SingletonDemo()</span>
            <span class="hljs-keyword">synchronized</span> (SingletonDemo.class) &#123;
                <span class="hljs-keyword">if</span> (instance == <span class="hljs-keyword">null</span>) instance = <span class="hljs-keyword">new</span> SingletonDemo();
            &#125;
        &#125;
        <span class="hljs-keyword">return</span> instance;
    &#125;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;
        <span class="hljs-comment">// 单线程（main线程的操作动作....） 构造函数只会执行一次</span>
<span class="hljs-comment">//        System.out.println(SingletonDemo.getInstance() == SingletonDemo.getInstance());</span>
<span class="hljs-comment">//        System.out.println(SingletonDemo.getInstance() == SingletonDemo.getInstance());</span>
<span class="hljs-comment">//        System.out.println(SingletonDemo.getInstance() == SingletonDemo.getInstance());</span>

        <span class="hljs-comment">// 并发多线程后，情况发生了很大的变化</span>
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt;= <span class="hljs-number">10</span>; i++) &#123;
            <span class="hljs-keyword">new</span> Thread(() -&gt; &#123;
                SingletonDemo.getInstance();
            &#125;, String.valueOf(i)).start();
        &#125;
    &#125;
&#125;</code></pre>

<h3 id="单例模式volatile分析"><a href="#单例模式volatile分析" class="headerlink" title="单例模式volatile分析"></a>单例模式volatile分析</h3><p><strong><font color=#FF000>DCL（Double Check Lock双端检锁机制）</font></strong> 不一定线程安全，原因是<font color=#FF000>有指令重排序的存在</font>，加入volatile可以禁止指令重排。<br>原因在于某一个线程执行到第一次检测，读取到的instance不为null时，instance的引用对象可能还没完成初始化。<br><strong>instance = new SingletonDemo();可分为一下3步完成（伪代码）：</strong></p>
<pre><code class="hljs java">memory = allocate();<span class="hljs-comment">//1.分配对象内存空间</span>
instance(memory);  <span class="hljs-comment">//2.初始化对象</span>
instance = memory ;<span class="hljs-comment">//3.设置instance指向刚刚分配的内存地址，此时instance!=null</span></code></pre>
<p>步骤2和步骤3<font color=#FF000>不存在数据依赖关系</font>，而且无论重排前还是重排后程序的执行结果在单线程中并没有改变，因此这种重排优化是允许的。</p>
<pre><code class="hljs java">memory = allocate();<span class="hljs-comment">//1.分配对象内存空间</span>
instance = memory ;<span class="hljs-comment">//3.设置instance指向刚刚分配的内存地址，此时instance!=null，但是对象还没有初始化完成！</span>
instance(memory);  <span class="hljs-comment">//2.初始化对象</span></code></pre>

<p>但是指令重排只会保证串行语义的执行的一致性（单线程），但并不会关心多线程间的语义一致性。<br><font color=#FF000>所以当一个线程访问instance不为null时，由于instance实例未必已初始化完成，也就造成了线程安全问题。</font></p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/%E9%9D%A2%E8%AF%95%E9%A2%98/">面试题</a>
                    
                      <a class="hover-with-bg" href="/tags/Java%E9%9D%A2%E8%AF%95%E9%A2%98/">Java面试题</a>
                    
                      <a class="hover-with-bg" href="/tags/JMM/">JMM</a>
                    
                      <a class="hover-with-bg" href="/tags/volatile/">volatile</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext row">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2020/11/10/java/CAS/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">CAS你知道吗？</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div id="tocbot"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://soldiergit.github.io/" target="_blank" rel="nofollow noopener"><span>Copyright © 2020 soldier Powered by Hexo Fluid Core on Kubernetes</span></a> <i class="iconfont icon-love"></i> <a href="mailto:soldier_wyyx@163.com"><span>联系我</span></a> 
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  

  
</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/main.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/lazyload.js" ></script>
  



  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/js/clipboard-use.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: '#post-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "请谈谈你对volatile的理解&nbsp;",
      ],
      cursorChar: "_",
      typeSpeed: 70,
      loop: false,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "hover",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    var path = "/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>












  
    <!-- Baidu Analytics -->
    <script defer>
      var _hmt = _hmt || [];
      (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?28d1b3a306f2b9f13ed66ffd0d5a0a9a";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
      })();
    </script>
  

  

  

  
    <!-- Tencent Analytics -->
    <script defer>
      var _mtac = {};
      (function () {
        var mta = document.createElement("script");
        mta.src = "//pingjs.qq.com/h5/stats.js?v2.0.4";
        mta.setAttribute("name", "MTAH5");
        mta.setAttribute("sid", "500732630");
        
        mta.setAttribute("cid", "500732632");
        
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(mta, s);
      })();
    </script>
  

  
    <!-- 51.la Analytics -->
    <script defer type="text/javascript" src="//js.users.51.la/20973747.js"></script>
  

  





</body>
</html>
